package net.homeip.mmhg.domain;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.joda.time.DateTime;

/**
 * 
 *
 * @author ilya gerasimov
 */
@SuppressWarnings("serial")
public class Task extends NamedEntity {
	
	private String puzzle;
	private String firstHint;
	private String secondHint;
	private boolean enabled = true;
	private DateTime blockingStart;
	private Integer capacity;
	private String answer;
	private List<String> answers;
	private Integer priority;

	public void setPuzzle(String puzzle) {
		this.puzzle = puzzle;
	}

	public String getPuzzle() {
		return puzzle;
	}

	public void setFirstHint(String firstHint) {
		this.firstHint = firstHint;
	}

	public String getFirstHint() {
		return firstHint;
	}

	public void setSecondHint(String secondHint) {
		this.secondHint = secondHint;
	}

	public String getSecondHint() {
		return secondHint;
	}

	public void setCapacity(Integer capacity) {
		this.capacity = capacity;
	}

	public Integer getCapacity() {
		return capacity;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setBlockingStart(DateTime blockingStart) {
		this.blockingStart = blockingStart;
	}

	public DateTime getBlockingStart() {
		return blockingStart;
	}
	
	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public String getAnswer() {
		return answer;
	}

	public boolean canBeObtained(int owners) {
		return enabled && ( (capacity == null) || (capacity == 0) || (capacity > owners) );
	}

	public void setPriority(Integer priority) {
		this.priority = priority;
	}

	public Integer getPriority() {
		return priority;
	}
	
	private List<String> parseAnswerString(String answer) {
		return new LinkedList<String>(Arrays.asList(answer.toUpperCase().split(" ")));
	}
	
	public List<String> getAnswers() {
		if (answers == null) {
			answers = parseAnswerString(answer);
		}
		return answers;
	}
	
	public int getCodeNumber() {
		return answers.size();
	}
}
